/**
 * @task 学习函数化构造器
 * @author wsq
 * @date 2022-4-13
 */
var mammal = function (spec) {
    var that = {};
    that.get_name = function() {
        return spec.name;
    };
    that.says = function () {
        return spec.saying || '';
    }
    return that;
}

var myMammal = mammal({name:'wsq'});
console.log(myMammal.get_name());  //wsq

// cat继承manmmal
var cat = function (spec) {
    spec.saying = spec.saying || 'hello';
    var that = mammal(spec);
    that.purr = function (n) {
        return 'this is purr';
    };
    that.get_name = function() {
        return that.says() + ' ' +spec.name +' ' + that.says();
    };
    return that;
};
var myCat = cat({name:'hero--'});
console.log(myCat.get_name());  // hello hero-- hello
console.log(myCat.says());  //hello
// console.log(myCat);  

//给函数增加 method 方法
Function.prototype.method= function (name,func){
    // 增加之前先判断是否已经存在
    if(!this.prototype[name]){
        this.prototype[name]=func;
    }
    return this;
}

// 构造 superior 方法 ，处理父类方法的方法
Object.method('superior',function (name){
    var that = this;
    // 当方法名是一个变量 使用[]调用
    method = that[name];
    return function () {
        return method.apply(that,arguments);
    };
});

var coolcat = function (spec) {
    var that = cat(spec),
      super_get_name = that.superior('get_name');
    that.get_name=function (n) {
        return 'like ' + super_get_name() + ' baby';
    };
    return that;
};
var myCoolCat = coolcat({name:'miao'});
var name = myCoolCat.get_name();
console.log(name);